define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yunfood/fund/pack/index' + location.search,
                    add_url: 'yunfood/fund/pack/add',
                    edit_url: 'yunfood/fund/pack/edit',
                    del_url: 'yunfood/fund/pack/del',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        {
                            field: 'state',
                            checkbox: true
                        },
                        {
                            field: 'id',
                            title: 'ID',
                        },
                        {
                            field: 'name_text',
                            title: __('名称'),
                        },
                        {
                            field: 'price',
                            title: __('售价'),
                            sortable: true,
                            operate: false
                        },
                        {
                            field: 'amount',
                            title: __('充值金额'),
                            sortable: true,
                            operate: false
                        },
                        {
                            field: 'user_limit',
                            title: __('次数限制'),
                            sortable: true,
                            operate: false
                        },
                        {
                            field: 'note',
                            title: __('备注'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索',
                        },
                        {
                            field: 'status',
                            title: __('Status'),
                            searchList: Config.statusList,
                            formatter: Table.api.formatter.label
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'order',
                                    text: __('订单'),
                                    title: function (row) {
                                        return __('相关订单') + ' - ' + row.name;
                                    },
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'yunfood/fund/order/index?fund_pack_id=' + row.id;
                                    },
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ],
                ],
                pagination: false,
                search: false,
                commonSearch: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {  // 渲染的方法
            },
            events: {  // 绑定事件的方法
            }
        }
    };
    return Controller;
});
